<template>
  <div class="main-content">
    <!-- 列表页 -->
    <template v-if="showFlag">
      <el-form
          class="center-form-pv"
          :inline="true"
          :model="searchForm"
      >
        <el-form-item label="来访分类">
          <el-select @change="laifangfenleiChange" clearable v-model="searchForm.laifangfenlei" placeholder="请选择来访分类">
            <el-option v-for="(item, index) in laifangfenleiOptions" :key="index" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-button
            type="primary"
            @click="search()"
            icon="el-icon-search"
        >查询</el-button
        >
        <el-button
            v-if="isAuth('laifangdengji', '新增')"
            type="success"
            @click="addOrUpdateHandler()"
            icon="el-icon-plus"
        >新增</el-button
        >
        <el-button
            v-if="isAuth('laifangdengji', '删除')"
            :disabled="dataListSelections.length <= 0"
            type="danger"
            icon="el-icon-delete"
            @click="deleteHandler()"
        >删除</el-button
        >
      </el-form>

      <!-- <div> -->
      <el-table
          class="tables"
          :stripe="true"
          v-if="isAuth('laifangdengji', '查看')"
          :data="dataList"
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandler"
      >
        <el-table-column :resizable="true" type="selection" align="center" width="50"></el-table-column>
        <el-table-column :resizable="true" :sortable="true" label="序号" type="index" width="50" />
        <el-table-column :resizable="true" :sortable="true" prop="laifangfenlei" label="来访分类">
          <template slot-scope="scope">
            {{ scope.row.laifangfenlei }}
          </template>
        </el-table-column>
        <el-table-column :resizable="true" :sortable="true" prop="laifangshijian" label="来访时间">
          <template slot-scope="scope">
            {{ scope.row.laifangshijian }}
          </template>
        </el-table-column>
        <el-table-column width="300" label="操作">
          <template slot-scope="scope">
            <el-button
                v-if="isAuth('laifangdengji', '查看')"
                plain
                circle
                icon="el-icon-view"
                type="success"
                size="mini"
                @click="addOrUpdateHandler(scope.row.id, 'info')"
            ></el-button
            >
            <el-button
                v-if="isAuth('laifangdengji', '修改')"
                type="primary"
                plain
                circle
                icon="el-icon-edit"
                size="mini"
                @click="addOrUpdateHandler(scope.row.id)"
            ></el-button
            >
            <el-button
                v-if="isAuth('laifangdengji', '删除')"
                type="danger"
                plain
                circle
                icon="el-icon-delete"
                size="mini"
                @click="deleteHandler(scope.row.id)"
            ></el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          background
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageSize"
          :layout="layouts.join()"
          :total="totalPage"
          prev-text="<"
          next-text=">"
          :hide-on-single-page="true"
      ></el-pagination>
      <!-- </div> -->
    </template>

    <!-- 添加/修改页面  将父组件的search方法传递给子组件-->
    <add-or-update v-if="addOrUpdateFlag" :parent="this" ref="addOrUpdate"></add-or-update>
  </div>
</template>

<script>
//$graphType1
//$buttonName1
//$subNameList1
import axios from 'axios';
import AddOrUpdate from './add-or-update';
export default {
  data() {
    return {
      searchForm: {
        key: '',
      },
      form: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      showFlag: true,
      sfshVisiable: false,
      shForm: {},
      chartVisiable: false,
      chartVisiable1: false,
      chartVisiable2: false,
      chartVisiable3: false,
      chartVisiable4: false,
      chartVisiable5: false,
      addOrUpdateFlag: false,
      layouts: ['total', 'prev', 'pager', 'next', 'sizes', 'jumper'],
    };
  },
  created() {
    this.init();
    this.getDataList();
    this.contentStyleChange();
  },
  mounted() {},
  filters: {
    htmlfilter: function (val) {
      return val.replace(/<[^>]*>/g).replace(/undefined/g, '');
    },
  },
  components: {
    AddOrUpdate,
  },
  methods: {
    contentStyleChange() {
      this.contentPageStyleChange();
    },
    // 分页
    contentPageStyleChange() {
      let arr = [];

      // if(this.contents.pageTotal) arr.push('total')
      // if(this.contents.pageSizes) arr.push('sizes')
      // if(this.contents.pagePrevNext){
      //   arr.push('prev')
      //   if(this.contents.pagePager) arr.push('pager')
      //   arr.push('next')
      // }
      // if(this.contents.pageJumper) arr.push('jumper')
      // this.layouts = arr.join()
      // this.contents.pageEachNum = 10
    },

    init() {
      this.laifangfenleiOptions = '志愿者,社会人员'.split(',');
    },
    search() {
      this.pageIndex = 1;
      this.getDataList();
    },

    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let params = {
        page: this.pageIndex,
        limit: this.pageSize,
        sort: 'id',
        order: 'desc',
      };
      if (this.searchForm.laifangfenlei !== '' && this.searchForm.laifangfenlei !== undefined) {
        params['laifangfenlei'] = this.searchForm.laifangfenlei;
      }
      if (this.searchForm.laifangshijianstart !== '' && this.searchForm.laifangshijianstart !== undefined) {
        params['laifangshijianstart'] = this.searchForm.laifangshijianstart;
      }
      if (this.searchForm.laifangshijianend !== '' && this.searchForm.laifangshijianend !== undefined) {
        params['laifangshijianend'] = this.searchForm.laifangshijianend;
      }
      this.$http({
        url: 'laifangdengji/page',
        method: 'get',
        params: params,
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.data.list;
          this.totalPage = data.data.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandler(val) {
      this.dataListSelections = val;
    },
    // 添加/修改
    addOrUpdateHandler(id, type) {
      this.showFlag = false;
      this.addOrUpdateFlag = true;
      this.crossAddOrUpdateFlag = false;
      if (type !== 'info') {
        type = 'else';
      }
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, type);
      });
    },
    // 下载
    download(file) {
      window.open(`${file}`);
    },
    // 删除
    deleteHandler(id) {
      var ids = id
          ? [Number(id)]
          : this.dataListSelections.map(item => {
            return Number(item.id);
          });
      this.$confirm(`确定进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$http({
          url: 'laifangdengji/delete',
          method: 'post',
          data: ids,
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.search();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  },
};
</script>
